<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keyword" placeholder="用户ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="状态" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="未审核" value="0" />
        <el-option label="已通过" value="1" />
        <el-option label="未通过" value="2" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;" onclick="drawer = true">{{ row.id }}</a>
        </template>
      </el-table-column>
      <el-table-column label="昵称" min-width="100" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.nickname }}</a>
        </template>
      </el-table-column>
      <el-table-column label="分类" min-width="80" align="center">
        <template slot-scope="{row}">
          <span>{{ row.category }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" min-width="50" align="center">
        <template slot-scope="{row}">
          <span>{{ row.type | formatType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="230" align="center">
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资源" min-width="50" align="center">
        <template slot-scope="{row}">
          <a class="click_a" @click="openResource(row)"> 查看 </a>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status | statusText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拒绝原因" min-width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.refuse_info }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="created_at" sortable="custom" min-width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <a v-if="row.status === 0" class="click_a" @click="agreePost(row.id)">
            通过
          </a>
          <a v-if="row.status === 0" class="click_a" @click="handleRefuse(row.id)">
            拒绝
          </a>
          <a v-if="row.status !== 3" class="click_a" @click="removePost(row.id)">
            删除
          </a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <div class="components-container">
      <el-dialog v-el-drag-dialog :visible.sync="dialogVisibleRefuse" title="拒绝理由" width="35%">
        <el-form label-width="80px" label-position="left">
          <el-form-item label="理由">
            <el-select v-model="refuse" placeholder="拒绝理由" class="filter-item" @change="handleFilter">
              <el-option v-for="refuse in refuses" :label="refuse" :value="refuse" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleRefuse = false">
            取消
          </el-button>
          <el-button type="primary" @click="refusePost">
            确定
          </el-button>
        </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogImages" title="图片" width="460px">
        <el-image v-for="image in images" :src="image" :preview-src-list="images" class="dialog_img" />
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogVideo" title="视频" width="460px" :before-close="dialogVideoHandle" style="text-align: center">
        <video controls class="dialog_video">
          <source :src="video" type="video/mp4">
        </video>
      </el-dialog>
      <el-dialog v-el-drag-dialog :visible.sync="dialogVoice" title="音频" width="460px" :before-close="dialogVoiceHandle" style="text-align: center">
        <audio controls class="dialog_voice">
          <source :src="voice" type="audio/mpeg">
        </audio>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import { postList, postAudit, postRemove } from '@/api/post'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    statusText(status) {
      const statusMap = {
        0: '待审核',
        1: '通过',
        2: '未通过'
      }
      return statusMap[parseInt(status)]
    },
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    },
    formatType: function(sex) {
      const sexMap = {
        1: '文字',
        2: '图片',
        3: '视频',
        4: '音频'
      }
      return sexMap[parseInt(sex)]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        status: '0',
        sort: 'desc',
        sort_field: 'id',
        keyword: ''
      },
      refuses: [
        '图片涉黄。',
        '图片带有联系方式，请保护好您的敏感信息。',
        '图片侵权或存在水印。',
        '图片存在敏感人物。',
        '图片存在暴力、恐怖血腥、反动内容。',
        '图片存在二维码或者广告信息。',
        '图片存在易引发他人不适。'
      ],
      dialogVisibleRefuse: false,
      dialogImages: false,
      dialogVideo: false,
      dialogVoice: false,
      images: false,
      video: '',
      voice: '',
      refuseId: 0,
      refuse: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      postList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    },
    agreePost(id) {
      postAudit({ id: id, status: 1 }).then((response) => {
        if (response.code === 1) {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
        }
      })
    },
    handleRefuse(id) {
      this.refuseId = id
      this.dialogVisibleRefuse = true
    },
    refusePost() {
      const data = {
        id: this.refuseId,
        status: 2,
        refuse_info: this.refuse
      }
      postAudit(data).then((response) => {
        if (response.code === 1) {
          this.$message({
            type: 'success',
            message: '审核成功'
          })
          this.dialogVisibleRefuse = false
          this.getList()
        } else {
          this.$message({
            type: 'error',
            message: response.msg
          })
        }
      })
    },
    removePost(id) {
      this.$confirm('确定要删除该动态?', 'Warning', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          postRemove({ id: id }).then((response) => {
            if (response.code === 1) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: '删除失败'
              })
            }
          })
        })
    },
    openResource(row) {
      if (row.type === 1) {
        this.$message({
          type: 'error',
          message: '没有资源'
        })
      }
      if (row.type === 2) {
        this.images = row.images
        this.dialogImages = true
      }
      if (row.type === 3) {
        this.video = row.video
        this.dialogVideo = true
      }
      if (row.type === 4) {
        this.voice = row.voice
        this.dialogVoice = true
      }
    },
    dialogVideoHandle(hide) {
      document.querySelector('video').pause()
      this.video = ''
      hide()
    },
    dialogVoiceHandle(hide) {
      document.querySelector('audio').pause()
      this.voice = ''
      hide()
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
.dialog_img {
  width: 100px;
  height: 100px;
  margin-right: 5px;
}
.dialog_video {
  height: 300px;
}
</style>
